/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.service.content;

import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.entity.HttpBinding;
import han.bifang.gwt.client.common.entity.JmsBindingWrapper;
import han.bifang.gwt.client.common.entity.SoapBinding;
import han.bifang.gwt.client.common.entity.WsdlWrapper;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.service.ServiceMessages;

import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;

/**
 * 
 * @Author Kent
 */
public class VersionCommContent extends Content {

	private BifangGWTServiceAsync service = null;

	private GwtContext context = null;

	private ServiceMessages cons;

	private VerticalPanel panel = null;

	private SimplePanel cp = null;
	

	public VersionCommContent(ServiceMessages cons,
			BifangGWTServiceAsync service, GwtContext context, Object b) {
		this.cons = cons;
		this.service = service;
		this.context = context;
		panel = new VerticalPanel();
		panel.setSpacing(5);
		panel.setWidth("100%");
		
		cp = new SimplePanel();
		cp.setWidth("100%");

		
		HorizontalPanel hp = new HorizontalPanel();
		hp.add(new HTML(cons.protocol() + ":&nbsp;&nbsp;&nbsp;"));

		RadioButton rb0 = new RadioButton("myRadioGroup", cons.http());
		RadioButton rb1 = new RadioButton("myRadioGroup", cons.webservices());
		RadioButton rb2 = new RadioButton("myRadioGroup", cons.jms());
		RadioButton rb3 = new RadioButton("myRadioGroup", cons.soap());

		if(b instanceof HttpBinding){
//			rb1.setEnabled(false);
//			rb2.setEnabled(false);
			rb0.setChecked(true);
			
		}else if(b instanceof SoapBinding){
//			rb1.setEnabled(false);
//			rb2.setEnabled(false);
			rb3.setChecked(true);
			
		}else if(b instanceof JmsBindingWrapper){
//			rb0.setEnabled(false);
//			rb1.setEnabled(false);
			rb2.setChecked(true);
		}else if(b instanceof WsdlWrapper){
//			rb0.setEnabled(false);
//			rb2.setEnabled(false);
			rb1.setChecked(true);
		}

		hp.add(rb0);
		hp.add(rb1);
		hp.add(rb2);
		hp.add(rb3);

		panel.add(hp);
		panel.add(cp);
		
		if(b instanceof HttpBinding){
			Grid http = new Grid(1, 2);
			http.setWidth("100%");
			http.setCellPadding(1);
			http.setCellSpacing(1);
			http.setBorderWidth(1);
			ColumnFormatter cf = http.getColumnFormatter();
			cf.setWidth(0, "15%");
			cf.setWidth(1, "55%");

			CellFormatter ff = http.getCellFormatter();
			ff.setStyleName(0, 0, "tablecol");

			http.setHTML(0, 0, cons.url());

			http.setHTML(0, 1, ((HttpBinding)b).getUrl());
			cp.setWidget(http);
		}else if(b instanceof SoapBinding){
			Grid soap = new Grid(2, 2);
			soap.setWidth("100%");
			soap.setCellPadding(1);
			soap.setCellSpacing(1);
			soap.setBorderWidth(1);
			ColumnFormatter cf = soap.getColumnFormatter();
			cf.setWidth(0, "15%");
			cf.setWidth(1, "55%");

			CellFormatter ff = soap.getCellFormatter();
			ff.setStyleName(0, 0, "tablecol");
			ff.setStyleName(1, 0, "tablecol");
			
			soap.setHTML(0, 0, cons.url());
			soap.setHTML(1, 0, cons.soapVersion());

			soap.setHTML(0, 1, ((SoapBinding)b).getUrl());
			soap.setHTML(1, 1, ((SoapBinding)b).getSoapVersion());
			
			cp.setWidget(soap);
		}else if(b instanceof JmsBindingWrapper){
			JmsBindingWrapper jb = (JmsBindingWrapper)b;
			ShowJmsPanel jms = new ShowJmsPanel(cons,jb);
			cp.setWidget(jms);
		}else if(b instanceof WsdlWrapper){
			WsdlWrapper w = (WsdlWrapper)b;
			
			FlexTable webservices = new FlexTable();
			webservices.setCellPadding(1);
			webservices.setCellSpacing(1);
			webservices.setBorderWidth(1);
			webservices.setWidth("100%");
			
			ColumnFormatter cf = webservices.getColumnFormatter();
			cf.setWidth(0, "15%");
			cf.setWidth(1, "55%");
			//cf.setWidth(1, "10%");

			FlexCellFormatter fff = webservices.getFlexCellFormatter();
			fff.setStyleName(0, 0, "tablecol");
			fff.setStyleName(1, 0, "tablecol");
			fff.setStyleName(2, 0, "tablecol");

			
			webservices.setHTML(0, 0, cons.wsdlUrl());
			webservices.setHTML(1, 0, cons.endpoint());
			webservices.setHTML(2, 0, cons.soapVersion());
			
			

			webservices.setHTML(0, 1, w.getWebServicesBinding().getWsdlUrl());
			
			webservices.setHTML(1, 1, w.getWebServicesBinding().getEndpointUrl());
			
			webservices.setHTML(2, 1, w.getWebServicesBinding().getSoapVersion());
			
			Grid wsp = new Grid(5, 1);
			wsp.setWidth("100%");
			wsp.setWidget(0, 0, webservices);
			
			wsp.setHTML(1, 0, "&nbsp;");
			
			ShowServicePanel sp = new ShowServicePanel(cons,w.getWebServicesBinding());
			wsp.setWidget(2, 0, sp);
			
			
			wsp.setWidget(3, 0, new HTML("&nbsp;"));
			ShowMethodPanel smp = new ShowMethodPanel(cons,w);
			wsp.setWidget(4, 0, smp);
			
			cp.setWidget(wsp);
			
		}

		initWidget(panel);

	}

	
}
